// baseUrl只是请求url的前半部分
import { baseUrl, imgUrl } from './url.js'

const request = function (options) {

    // 获取放入缓存的字段token
    var jkcs = { 'Content-Type': 'application/json' };
    var osscs = { 'token': `${uni.getStorageSync("access_token")}` };

    if (uni.getStorageSync("access_token")) {
        jkcs.token = uni.getStorageSync("access_token");
    }
    if (uni.getStorageSync("user").corpBaseInfoResponse) {
        jkcs.corp = uni.getStorageSync("user").corpBaseInfoResponse.id;
    }
    if (uni.getStorageSync("dqxm").id) {
        jkcs.project = uni.getStorageSync("dqxm").id;
    }

    uni.setStorageSync("jkcs", jkcs);
    // console.log("212121", uni.getStorageSync("jkcs"));

    uni.setStorageSync("osscs", osscs);

    if (options.oss) {
        console.log("1111111", imgUrl);

        options.url = imgUrl;
        options.header = uni.getStorageSync("osscs");
        options.name = 'file';
    } else {
        options.url = baseUrl + options.url;
        options.header = uni.getStorageSync("jkcs");

    }



    // 这里对response进行处理，
    // 401表示登录状态过期，需重新登录
    options.complete = (response) => {
        if (response.data.code == 401) {
            wx.showToast({
                title: '请重新登录',
                icon: 'none',
                duration: 1500,
                success: function () {
                    setTimeout(function () {
                        uni.navigateTo({
                            url: '/pages/login/login'
                        });
                    }, 2000);
                }
            });
            return;
        }
        if (response.data.success == false) {
            uni.showToast({
                title: response.data.msg,
                icon: 'none'
            });
            return;
        }

    }

    // 服务器出错等信息
    options.fail = () => {
        uni.showToast({
            title: '服务器开小差，请稍后~',
            icon: 'none'
        });
        return false;
    }

    if (options.oss) {
        return uni.uploadFile(options);
    } else {
        return uni.request(options);
    }


}
export default request;